|
1
|
|
|
export default renderSVG; |
|
2
|
|
|
|
|
3
|
|
|
import merge from "../help/merge.js"; |
|
4
|
|
|
import {getEncodingHeight, getBarcodePadding} from "./shared.js"; |
|
5
|
|
|
|
|
6
|
|
|
var svgns = "http://www.w3.org/2000/svg"; |
|
7
|
|
|
|
|
8
|
|
|
function renderSVG(svg, encodings, options){ |
|
9
|
|
|
var currentX = options.marginLeft; |
|
10
|
|
|
|
|
11
|
|
|
prepareSVG(svg, options, encodings); |
|
12
|
|
|
for(let i = 0; i < encodings.length; i++){ |
|
13
|
|
|
var encodingOptions = merge(options, encodings[i].options); |
|
14
|
|
|
|
|
15
|
|
|
var group = createGroup(currentX, encodingOptions.marginTop, svg); |
|
16
|
|
|
|
|
17
|
|
|
setGroupOptions(group, encodingOptions); |
|
18
|
|
|
|
|
19
|
|
|
drawSvgBarcode(group, encodingOptions, encodings[i]); |
|
20
|
|
|
drawSVGText(group, encodingOptions, encodings[i]); |
|
21
|
|
|
|
|
22
|
|
|
currentX += encodings[i].width; |
|
23
|
|
|
} |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
function prepareSVG(svg, globalOptions, encodings){ |
|
28
|
|
|
// Clear the SVG |
|
29
|
|
|
while (svg.firstChild) { |
|
30
|
|
|
svg.removeChild(svg.firstChild); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
var totalWidth = 0; |
|
34
|
|
|
var maxHeight = 0; |
|
35
|
|
|
for(let i = 0; i < encodings.length; i++){ |
|
36
|
|
|
var options = merge(globalOptions, encodings[i].options); |
|
37
|
|
|
|
|
38
|
|
|
// Calculate the width of the encoding |
|
39
|
|
|
var textWidth = messureSVGtext(encodings[i].text, svg, options); |
|
40
|
|
|
var barcodeWidth = encodings[i].data.length * options.width; |
|
41
|
|
|
encodings[i].width = Math.ceil(Math.max(textWidth, barcodeWidth)); |
|
42
|
|
|
|
|
43
|
|
|
// Calculate the height of the encoding |
|
44
|
|
|
var encodingHeight = getEncodingHeight(encodings[i], options); |
|
45
|
|
|
|
|
46
|
|
|
encodings[i].barcodePadding = getBarcodePadding(textWidth, barcodeWidth, options); |
|
47
|
|
|
|
|
48
|
|
|
if(encodingHeight > maxHeight){ |
|
49
|
|
|
maxHeight = encodingHeight; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
totalWidth += encodings[i].width; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
var width = totalWidth + globalOptions.marginLeft + globalOptions.marginRight; |
|
56
|
|
|
var height = maxHeight; |
|
57
|
|
|
|
|
58
|
|
|
svg.setAttribute("width", width + "px"); |
|
59
|
|
|
svg.setAttribute("height", height + "px"); |
|
60
|
|
|
svg.setAttribute("x", "0px"); |
|
61
|
|
|
svg.setAttribute("y", "0px"); |
|
62
|
|
|
svg.setAttribute("viewBox", "0 0 " + width + " " + height); |
|
63
|
|
|
|
|
64
|
|
|
svg.setAttribute("xmlns", svgns); |
|
65
|
|
|
svg.setAttribute("version", "1.1"); |
|
66
|
|
|
|
|
67
|
|
|
svg.style.transform = "translate(0,0)"; |
|
68
|
|
|
|
|
69
|
|
|
if(globalOptions.background){ |
|
70
|
|
|
svg.style.background = globalOptions.background; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
function drawSvgBarcode(parent, options, encoding){ |
|
75
|
|
|
var binary = encoding.data; |
|
76
|
|
|
|
|
77
|
|
|
// Creates the barcode out of the encoded binary |
|
78
|
|
|
var yFrom; |
|
79
|
|
|
if(options.textPosition == "top"){ |
|
80
|
|
|
yFrom = options.fontSize + options.textMargin; |
|
81
|
|
|
} |
|
82
|
|
|
else{ |
|
83
|
|
|
yFrom = 0; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
var barWidth = 0; |
|
87
|
|
|
var x; |
|
88
|
|
|
for(var b = 0; b < binary.length; b++){ |
|
89
|
|
|
x = b * options.width + encoding.barcodePadding; |
|
90
|
|
|
|
|
91
|
|
|
if(binary[b] === "1"){ |
|
92
|
|
|
barWidth++; |
|
93
|
|
|
} |
|
94
|
|
|
else if(barWidth > 0){ |
|
95
|
|
|
drawLine(x - options.width * barWidth, yFrom, options.width * barWidth, options.height, parent); |
|
96
|
|
|
barWidth = 0; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// Last draw is needed since the barcode ends with 1 |
|
101
|
|
|
if(barWidth > 0){ |
|
102
|
|
|
drawLine(x - options.width * (barWidth - 1), yFrom, options.width * barWidth, options.height, parent); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
function drawSVGText(parent, options, encoding){ |
|
107
|
|
|
var textElem = document.createElementNS(svgns, 'text'); |
|
108
|
|
|
|
|
109
|
|
|
// Draw the text if displayValue is set |
|
110
|
|
|
if(options.displayValue){ |
|
111
|
|
|
var x, y; |
|
112
|
|
|
|
|
113
|
|
|
textElem.setAttribute("style", |
|
114
|
|
|
"font:" + options.fontOptions + " " + options.fontSize + "px " + options.font |
|
115
|
|
|
); |
|
116
|
|
|
|
|
117
|
|
|
if(options.textPosition == "top"){ |
|
118
|
|
|
y = options.fontSize - options.textMargin; |
|
119
|
|
|
} |
|
120
|
|
|
else{ |
|
121
|
|
|
y = options.height + options.textMargin + options.fontSize; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
// Draw the text in the correct X depending on the textAlign option |
|
125
|
|
|
if(options.textAlign == "left" || encoding.barcodePadding > 0){ |
|
126
|
|
|
x = 0; |
|
127
|
|
|
textElem.setAttribute("text-anchor", "start"); |
|
128
|
|
|
} |
|
129
|
|
|
else if(options.textAlign == "right"){ |
|
130
|
|
|
x = encoding.width - 1; |
|
131
|
|
|
textElem.setAttribute("text-anchor", "end"); |
|
132
|
|
|
} |
|
133
|
|
|
// In all other cases, center the text |
|
134
|
|
|
else{ |
|
135
|
|
|
x = encoding.width / 2; |
|
136
|
|
|
textElem.setAttribute("text-anchor", "middle"); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
textElem.setAttribute("x", x); |
|
140
|
|
|
textElem.setAttribute("y", y); |
|
141
|
|
|
|
|
142
|
|
|
textElem.appendChild(document.createTextNode(encoding.text)); |
|
143
|
|
|
|
|
144
|
|
|
parent.appendChild(textElem); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
// |
|
149
|
|
|
// Help functions |
|
150
|
|
|
// |
|
151
|
|
|
function messureSVGtext(string, svg, options){ |
|
152
|
|
|
// Create text element |
|
153
|
|
|
/* var text = document.createElementNS(svgns, 'text'); |
|
154
|
|
|
text.style.fontFamily = options.font; |
|
155
|
|
|
|
|
156
|
|
|
text.setAttribute("style", |
|
157
|
|
|
"font-family:" + options.font + ";" + |
|
158
|
|
|
"font-size:" + options.fontSize + "px;" |
|
159
|
|
|
); |
|
160
|
|
|
|
|
161
|
|
|
var textNode = document.createTextNode(string); |
|
162
|
|
|
|
|
163
|
|
|
text.appendChild(textNode); |
|
164
|
|
|
|
|
165
|
|
|
svg.appendChild(text); |
|
166
|
|
|
|
|
167
|
|
|
var size = text.getComputedTextLength(); |
|
168
|
|
|
|
|
169
|
|
|
svg.removeChild(text); |
|
170
|
|
|
*/ |
|
171
|
|
|
// TODO: Use svg to messure the text width |
|
172
|
|
|
// Set font |
|
173
|
|
|
var ctx = document.createElement("canvas").getContext("2d"); |
|
174
|
|
|
ctx.font = options.fontOptions + " " + options.fontSize + "px " + options.font; |
|
175
|
|
|
|
|
176
|
|
|
// Calculate the width of the encoding |
|
177
|
|
|
var size = ctx.measureText(string).width; |
|
178
|
|
|
|
|
179
|
|
|
return size; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
function createGroup(x, y, svg){ |
|
183
|
|
|
var group = document.createElementNS(svgns, 'g'); |
|
184
|
|
|
|
|
185
|
|
|
group.setAttribute("transform", "translate(" + x + ", " + y + ")"); |
|
186
|
|
|
|
|
187
|
|
|
svg.appendChild(group); |
|
188
|
|
|
|
|
189
|
|
|
return group; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
function setGroupOptions(group, options){ |
|
193
|
|
|
group.setAttribute("style", |
|
194
|
|
|
"fill:" + options.lineColor + ";" |
|
195
|
|
|
); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
function drawLine(x, y, width, height, parent){ |
|
199
|
|
|
var line = document.createElementNS(svgns, 'rect'); |
|
200
|
|
|
|
|
201
|
|
|
line.setAttribute("x", x); |
|
202
|
|
|
line.setAttribute("y", y); |
|
203
|
|
|
line.setAttribute("width", width); |
|
204
|
|
|
line.setAttribute("height", height); |
|
205
|
|
|
|
|
206
|
|
|
parent.appendChild(line); |
|
207
|
|
|
} |
|
208
|
|
|
|